﻿using Newtonsoft.Json.Linq;
using System;
using System.Net;

namespace scanner_tron_v2
{
    //连入tron 网络，先把所有交易id扒下来

    //v3 继续用交易id 去查地址1
    internal class Program
    {
        static string[] ips ={
    "3.225.171.164",
    "52.53.189.99",
    "18.196.99.16",
    "34.253.187.192",
    "18.133.82.227",
    "35.180.51.163",
    "54.252.224.209",
    "18.231.27.82",
    "52.15.93.92",
    "34.220.77.106",
    "15.207.144.3",
    "13.124.62.58",
    "13.229.128.108",
    "35.182.37.246",
    "34.200.228.125",
    "18.220.232.201",
    "13.57.30.186",
    "35.165.103.105",
    "18.184.238.21",
    "34.250.140.143",
    "35.176.192.130",
    "52.47.197.188",
    "52.62.210.100",
    "13.231.4.243",
    "18.231.76.29",
    "35.154.90.144",
    "13.125.210.234",
    "13.250.40.82",
    "35.183.101.48"};


        const int linecount = 100;
        static async void TaskMain()
        {


            //Console.WriteLine("height=" + Finder.RemoteHeight);

            if (System.IO.Directory.Exists("data") == false)
            {
                System.IO.Directory.CreateDirectory("data");
            }
            if (System.IO.Directory.Exists("dataaddr2") == false)
            {
                System.IO.Directory.CreateDirectory("dataaddr2");
            }
            if (System.IO.Directory.Exists("indext") == false)
            {
                System.IO.Directory.CreateDirectory("indext");
            }
            //Console.Clear();

            for (var i = 0; i < linecount; i++)
            {
                var liner = new Liner(i);
                RunLiner(liner, i, GetParsedHeight(i));
            }

            Finder.BeginTestIP(ips);
            LogIPS();
        }
        static async void LogIPS()
        {
            while (true)
            {
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("ips=" + "(used/free/test)" + Finder.usePoints + "/" + Finder.freePoints.Count + "/" + Finder.testips.Count + "  height=" + Finder.RemoteHeight + "    "); ;
                await Task.Delay(500);
            }


        }
        static async void RunLiner(Liner liner, int index, FileSeeker seek)
        {
            List<string> addrs = new List<string>();
            if (seek.lineid != index)
            {
                throw new Exception("Error Liner.");
            }

            while (seek.fileid < 100)
            {
                if (seek.GetLine() != null)
                {
                    addrs.Clear();
                    await liner.FindAddrInTx(seek.GetLine(), addrs);
                    if (addrs.Count != 0)
                    {
                        AppendAddr(index, seek.fileid, addrs);
                    }

                    int y = index / 5 + 5;
                    int x = index % 5;
                    Console.SetCursorPosition(x * 30, y);
                    var log = seek.ToString();
                    while (log.Length < 10)
                        log += " ";
                    Console.WriteLine(log);
                }
            }

        }
        //分100条线跑，

        public class FileSeeker
        {
            public int lineid;
            public int fileid;
            const string fileprefix = "data\\tx_line";
            public string[] alllines;
            public int lineindex;


            public string GetLine()
            {

                if (alllines == null)
                {
                    var file = fileprefix + lineid.ToString("D04") + "_" + (fileid) + ".txt";
                    alllines = System.IO.File.ReadAllLines(file);
                }
                if (alllines.Length > lineindex)
                {
                    var str = alllines[lineindex];
                    lineindex++;
                    if(lineindex%100==0)
                    {
                        Save();
                    }
                    return str;
                }
                else
                {
                    lineindex = 0;
                    fileid++;
                    alllines = null;
                    Save();
                    return null;
                }
            }
            void Save()
            {
                var file = "indext/indext_" + lineid.ToString("D04") + ".txt";
                System.IO.File.WriteAllLines(file, new string[]
                {
                    this.fileid.ToString(),
                    this.lineindex.ToString(),
                    ""
                }
                );
            }
            public void Resume(int line)
            {
                this.lineid = line;
                var file = "indext/indext_" + lineid.ToString("D04") + ".txt";
                var linex = System.IO.File.ReadAllLines(file);
                this.fileid = int.Parse(linex[0]);
                this.lineindex = int.Parse(linex[1]);
            }
            public override string ToString()
            {
                int lc = 0;
                if (alllines != null)
                     lc = alllines.Length;
                return lineid.ToString("D2") + ":"+fileid.ToString("D04")+ " " + lineindex + "/" + lc+"  ";
            }

        }
        static FileSeeker[] localheight = null;

        static FileSeeker GetParsedHeight(int line)
        {
            if (localheight == null)
            {
                localheight = new FileSeeker[linecount];
                for (var i = 0; i < linecount; i++)
                {
                    try
                    {
                        localheight[i] = new FileSeeker();
                        localheight[i].Resume(i);

                    }
                    catch
                    {
                        localheight[i].lineid = i;
                        localheight[i].fileid = 0;
                    }
                }
            }
            return localheight[line];
        }

        static void AppendAddr(int line, int fileid, List<string> result)
        {
            var filename = "dataaddr2/" + "addr_line" + line.ToString("D04") + "_" + fileid + ".txt";
            System.IO.File.AppendAllLines(filename, result);
        }

        static void Main(string[] args)
        {
            TaskMain();

            while (
                true)
            {
                Console.ReadLine();
            }
        }
    }
}